﻿using System;
using System.Collections.Generic;

using System.Text;
using System.IO;

namespace ShapeMergeFactory
{
    internal class ShapeFileHead
    {
        private BoundingBox boundingBox
        {
            get;
            set;
        }
        private int fileCode;
        private int unused1;
        private int unused2;
        private int unused3;
        private int unused4;
        private int unused5;       
        private int version;
        private int shapeType;
       
        public int FileLength { private set; get; }

        public ShapeFileHead()
        {
            fileCode = 9994;
            unused1 = 0;
            unused2 = 0;
            unused3 = 0;
            unused4 = 0;
            unused5 = 0;
            FileLength = 100;
            version = 1000;
            shapeType = 5;
            boundingBox = new BoundingBox();
        }

        public void Read(ShapeBinaryReader binaryReader)
        {
            fileCode = binaryReader.BinaryReadBigEndian();
            unused1 = binaryReader.BinaryReadBigEndian();
            unused2 = binaryReader.BinaryReadBigEndian();
            unused3 = binaryReader.BinaryReadBigEndian();
            unused4 = binaryReader.BinaryReadBigEndian();
            unused5 = binaryReader.BinaryReadBigEndian();
            FileLength = binaryReader.BinaryReadBigEndian();
            version = binaryReader.ReadInt32();
            shapeType = binaryReader.ReadInt32();
            boundingBox.Read(binaryReader);
        }

        public void Write(ShapeBinaryWriter binaryWriter)
        {
            binaryWriter.BinaryWriteBigEndian(fileCode);
            binaryWriter.BinaryWriteBigEndian(unused1);
            binaryWriter.BinaryWriteBigEndian(unused2);
            binaryWriter.BinaryWriteBigEndian(unused3);
            binaryWriter.BinaryWriteBigEndian(unused4);
            binaryWriter.BinaryWriteBigEndian(unused5);
            binaryWriter.BinaryWriteBigEndian(FileLength);
            binaryWriter.Write(version);
            binaryWriter.Write(shapeType);
            boundingBox.Write(binaryWriter);
        }

        public void Merge(ShapeFileHead fileHeader)
        {
            FileLength = FileLength + fileHeader.FileLength - 100;
            boundingBox.Merge(fileHeader.boundingBox);
        }
    }
}
